package com.me.components {
	import com.me.FileLoader;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.system.IME;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;

	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 下午03:48:48
	 */
	public class Avatar extends MapResource {
		protected var _spName:TextField = new TextField();
		protected var _spBit:Bitmap;
		protected var _dragHandler:Function;
		protected var _stopDragHandler:Function;

		protected var _shandowSp:SpriteVisualElement = new SpriteVisualElement();
		protected var _body:Bitmap = new Bitmap();

		protected var frameTimes:Number = 0;
		protected var currentFrame:int = 0;
		protected var _loop:int = 0;

		public var frames:Array;
		public var sign:String;
		public var action:String;
		public var loadComplete:Boolean = false;
		public var frame:int = 1;
		public var offX:int = -26;
		public var offY:int = -111;
		public var frameTime:int = 100000;
		public var nameX:int = 0;
		public var nameY:int = -130;
		public var shadowX:Number = 1;
		public var shadowY:Number = 1;

		//<data X="-29" Y="-122" Frame="23" Width="68" Height="126" Time="160" NameX="1" NameY="-136" shadowX="150" shadowY="100" hitWidth="30" hitHeight="30" action="0"/>

		public function Avatar(file:File, handler:Function, dragHandler:Function = null, stopDragHandler:Function = null) {
			super();
			_fileName = file.name;
			_loadCompleteHandler = handler;
			_dragHandler = dragHandler;
			_stopDragHandler = stopDragHandler;
			_spName.width = 60;
			_spName.type = TextFieldType.INPUT;
			_spName.autoSize = TextFieldAutoSize.CENTER;
			_spName.background = true;
			_spName.backgroundColor = 0xff0000;
			_spName.text = "NPC";
			_spName.addEventListener(FocusEvent.FOCUS_IN, function(e:FocusEvent):void {
				IME.enabled = true;
			});
			_shandowSp.graphics.clear();
			_shandowSp.graphics.beginFill(0xFF0000, 1);
			_shandowSp.graphics.drawEllipse(-25, -15, 50, 30);
			_shandowSp.graphics.endFill();
			_shandowSp.graphics.lineStyle(2, 1);
			_shandowSp.graphics.drawRect(-1, -1, 2, 2);
			addChild(_spName);
			addChild(_shandowSp);
			addChild(_body);
			FileLoader.getInstance().loadFile(file, fileLoaded);
		}

		private function initShandow():void {
//			_shandowSp.scaleX = (Number(this.shadowX) * 0.01);
//			_shandowSp.scaleY = (Number(this.shadowY) * 0.01);
		}

		private function initBody():void {
			if (frames.length > 0) {
				_body.bitmapData = frames[0];
			}
			_body.x = this.offX;
			_body.y = this.offY;
		}

		override public function pass(delta:Number):void {
			if (this.frames.length == 1) {
				return;
			}
			;
			this.frameTimes += delta;
			if (this.frameTimes >= this.frameTime) {
				this.frameTimes = (this.frameTimes % this.frameTime);
				this.currentFrame = (this.currentFrame + 1) % this.frames.length;
				if (this.currentFrame >= this.frames.length) {
					this._loop++;
				}
				this._body.bitmapData = this.frames[this.currentFrame];
			}
			;
		}

		private function fileLoaded(data:Object):void {
			try {
				this.frames = data.list;
				parse(data.xml);
				initShandow();
				initBody();
				_spName.x = this.nameX - _spName.textWidth >> 1;
				_spName.y = this.nameY;
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
				_loadCompleteHandler(this);
			} catch (e:Error) {

			}
		}

		public function parse(xml:XML):void {
			this.frame = int(xml.@Frame);
			this.frameTime = int(xml.@Time);
			this.offX = int(xml.@X);
			this.offY = int(xml.@Y);
			this.nameX = int(xml.@NameX);
			this.nameY = (int(xml.@NameY) - 8);
			this.shadowX = Number(xml.@shadowX);
			this.shadowY = Number(xml.@shadowY);
			if (this.shadowX > 20) {
				this.shadowX = (this.shadowX * 0.01);
			}
			;
			if (this.shadowY > 20) {
				this.shadowY = (this.shadowY * 0.01);
			}
			;
		}

		private function onMouseDownHandler(e:MouseEvent):void {
			if (stage) {
				stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
				startDrag(false);
				if (_dragHandler != null) {
					_dragHandler();
				}
			}
		}

		private function onMouseUpHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			stopDrag();
			if (_stopDragHandler != null) {
				_stopDragHandler();
			}
		}
	}
}
